package com.ht.dsp;

import com.ht.aems.dbservice.ConnectionFactory;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Liu Kaicheng
 *         Date: 2011-12-14
 */
public class Pro104DataFiller {
    private Connection c;

    public void setMAX_INTERRUPT_ALLOWED(int MAX_INTERRUPT_ALLOWED) {
        this.MAX_INTERRUPT_ALLOWED = MAX_INTERRUPT_ALLOWED;
    }

    private int MAX_INTERRUPT_ALLOWED = 60;

    public static void main(String[] args) {
        ConnectionFactory.setAccessInfo("jdbc:oracle:thin:@172.11.2.203:1521:shaems",
                "shdata", "123", "oracle.jdbc.driver.OracleDriver");
        Pro104DataFiller filler = new Pro104DataFiller(ConnectionFactory.createConnection());
        Calendar c = Calendar.getInstance();
        c.set(2012, 6, 1);
        Calendar c1 = Calendar.getInstance();
        c1.set(2012, 6, 3);
        try {
            filler.fillDiscretePoints(c.getTimeInMillis(), c1.getTimeInMillis());
            //filler.fillDiscretePoints(c.getTimeInMillis(), System.currentTimeMillis());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                filler.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public Pro104DataFiller(Connection c) {
        this.c = c;
    }

    public void fillDiscretePoints(long startTime, long endTime) throws SQLException {
        fillDiscretePoints(startTime, endTime, false);
    }

    public void fillDiscretePoints(long startTime, long endTime, boolean isAccurateTime) throws SQLException {
        Statement s = c.createStatement();
        Statement s1 = c.createStatement();
        String tableName = "discrete_" + MySqlToOracle.format.format(new Date(startTime));
        ArrayList<Integer> ids = getDiscreteIds(tableName);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(startTime / 1000 * 1000);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        startTime = c.getTimeInMillis();
        int currentMinute;
        while (startTime <= endTime) {
            tableName = "discrete_" + MySqlToOracle.format.format(new Date(startTime));
            System.out.println("updating table " + tableName + " ...");
            for (int id : ids) {
                ResultSet result = s.executeQuery("select * from " + tableName + " where nid = " + id + " order by nminute");
                int lastMinute = -5;
                int lastValue = -1;
                while (result.next()) {
                    int minute = result.getInt("nminute");
                    int value = result.getInt("nvalue");
                    if (minute - lastMinute > 5 && minute - lastMinute <= MAX_INTERRUPT_ALLOWED) {
                        if(lastValue == -1)
                            lastValue = value;
                        for (currentMinute = lastMinute / 5 * 5 + 5; currentMinute < minute; currentMinute += 5)
                            s1.executeUpdate("insert into " + tableName + " values(" + id + "," + currentMinute + "," + lastValue + ")");
                    }
                    lastMinute = minute;
                    lastValue = value;
                }
                if (!isAccurateTime) {
                    if (lastMinute < 1435 && lastMinute >= 1440 - MAX_INTERRUPT_ALLOWED && (startTime + MySqlToOracle.MS_OF_ONE_DAY <= endTime))
                        for (currentMinute = lastMinute / 5 * 5 + 5; currentMinute < 1435; currentMinute += 5)
                            s1.executeUpdate("insert into " + tableName + " values(" + id + "," + currentMinute + "," + lastValue + ")");
                } else {
                    for (currentMinute = lastMinute / 5 * 5 + 5; currentMinute < (endTime - startTime) / MySqlToOracle.MS_OF_ONE_MINUTE; currentMinute += 5)
                        s1.executeUpdate("insert into " + tableName + " values(" + id + "," + currentMinute + "," + lastValue + ")");
                }
                result.close();
            }
            startTime += MySqlToOracle.MS_OF_ONE_DAY;
        }
        s.close();
        s1.close();
    }

    public ArrayList<Integer> getDiscreteIds(String tableName) throws SQLException {
        Statement s = c.createStatement();
        if (tableName == null || tableName.trim().equals(""))
            tableName = "discrete_" + MySqlToOracle.format.format(new Date(System.currentTimeMillis()));
        String sql = "select distinct nid from " + tableName;
        ResultSet result = s.executeQuery(sql);
        ArrayList<Integer> ids = new ArrayList<Integer>();
        while (result.next())
            ids.add(result.getInt("nid"));
        result.close();
        s.close();
        return ids;
    }

    public void closeConnection() throws SQLException {
        c.close();
    }
}
